//
//  SetPayWordViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/11.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "SetPayWordViewController.h"
#import "MyWalletViewController.h"
#import "PayViewController.h"
@interface SetPayWordViewController ()

@property(nonatomic ,strong) UITextField *oldPassWord;
@property(nonatomic ,strong) UITextField *passWord;
@property(nonatomic ,strong) UITextField *passWord1;

@end

@implementation SetPayWordViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];
}
-(void)backAction{
    UIViewController *viewCtl = self.navigationController.viewControllers[1];
    [self.navigationController popToViewController:viewCtl animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.statue==1||self.statue==10) {
        self.title=MyString(@"设置支付密码");
    }else if (self.statue==2){
        if (self.type==2) {
           self.title=MyString(@"修改登录密码");
        }else{
           self.title=MyString(@"修改支付密码");
        }
        
    }else{
        self.title=MyString(@"修改支付密码");
    }
    
    // Do any additional setup after loading the view.
}
-(void)createView{
    CGFloat height;
    CGFloat width=MainW-20;
    self.oldPassWord=[[UITextField alloc]initWithFrame:CGRectMake(10, 30, MainW-20, 49)];
    self.oldPassWord.placeholder=MyString(@"请输入原有支付密码");
    [self.oldPassWord setValue:[UIFont systemFontOfSize:14]forKeyPath:@"_placeholderLabel.font"];
    self.oldPassWord.keyboardType=UIKeyboardTypeNumberPad;
    self.oldPassWord.borderStyle=UITextBorderStyleRoundedRect;
    [self.view addSubview:self.oldPassWord];
    
    self.passWord=[[UITextField alloc]initWithFrame:CGRectMake(10, 30, MainW-20, 49)];
    self.passWord.placeholder=MyString(@"请设置您的支付密码");
    [self.passWord setValue:[UIFont systemFontOfSize:14]forKeyPath:@"_placeholderLabel.font"];
    self.passWord.keyboardType=UIKeyboardTypeNumberPad;
    self.passWord.borderStyle=UITextBorderStyleRoundedRect;
    [self.view addSubview:self.passWord];

    
    self.passWord1=[[UITextField alloc]initWithFrame:CGRectMake(10, 110, MainW-20, 49)];
    self.passWord1.placeholder=MyString(@"请再次输入密码");
    [self.passWord1 setValue:[UIFont systemFontOfSize:14]forKeyPath:@"_placeholderLabel.font"];
    self.passWord1.keyboardType=UIKeyboardTypeNumberPad;
    self.passWord1.borderStyle=UITextBorderStyleRoundedRect;
    [self.view addSubview:self.passWord1];
    
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 170, MainW, 20)];
    if (self.type==2) {
        label.text=MyString(@"备注: 请将密码设置为6-20位,并由数字,字母和符号两种以上的组合");
        label.numberOfLines=2;
       self.oldPassWord.keyboardType=UIKeyboardTypeDefault;
       self.passWord1.keyboardType=UIKeyboardTypeDefault;
       self.passWord.keyboardType=UIKeyboardTypeDefault;

    }else{
        label.text=MyString(@"备注: 请将密码设置为6位数字");
    }
    label.font=FontAndStyle12;
    label.textColor=TextColor9;
    [self.view addSubview:label];
    
    UIButton *MySureBtn=[[UIButton alloc]initWithFrame:CGRectMake(10, 220, MainW-20, 50)];
    [MySureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [MySureBtn setTitle:MyString(@"确认") forState:UIControlStateNormal];
    [MySureBtn addTarget:self action:@selector(SureClick) forControlEvents:UIControlEventTouchUpInside];
    [MySureBtn setBackgroundColor:Red_Color];
    [self.view addSubview:MySureBtn];
    ViewRadius(MySureBtn, 5);
    if (self.statue==1||self.statue==10) {
        self.oldPassWord.hidden=YES;
        height=30;
    }else if (self.statue==2){
        height=110;
        self.passWord.placeholder=MyString(@"请重新设置支付密码");
        self.passWord1.placeholder=MyString(@"请再次输入支付密码");
        self.oldPassWord.hidden=NO;
    }else{
        self.passWord.placeholder=MyString(@"请重新设置支付密码");
        self.oldPassWord.hidden=YES;
        height=30;
    }
    if (self.type==2) {
        self.passWord.placeholder=MyString(@"请重新输入登录密码");
        self.passWord1.placeholder=MyString(@"请再次输入登录密码");
        self.oldPassWord.placeholder=MyString(@"请输入旧密码");

    }
    [self.passWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(height);
        make.left.equalTo(10);
        make.height.equalTo(49);
        make.width.equalTo(width);
    }];
    [self.passWord1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passWord.mas_bottom).offset(30);
        make.left.equalTo(10);
        make.height.equalTo(49);
        make.width.equalTo(width);
    }];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passWord1.mas_bottom).offset(10);
        make.left.equalTo(10);
        make.height.equalTo(49);
        make.width.equalTo(width);
    }];
    [MySureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom).offset(30);
        make.left.equalTo(10);
        make.height.equalTo(49);
        make.width.equalTo(width);
    }];
    
  }
- (BOOL)isNum:(NSString *)checkedNumString {
	checkedNumString = [checkedNumString stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
	if(checkedNumString.length > 0) {
		return NO;
	}
	return YES;
}


-(void)SureClick{
    
    if (self.statue==1||self.statue==10) {
        if (self.passWord1.text.length==0||self.passWord.text.length==0) {
            [MBProgressHUD showError:MyString(@"密码不能为空")];
		}else if ([self isNum:self.passWord1.text] == NO || [self isNum:self.passWord.text] == 0){
			[MBProgressHUD showError:MyString(@"密码只能是数字")];

		}else{
            if (self.passWord.text.length!=6||self.passWord1.text.length!=6) {
                [MBProgressHUD showError:MyString(@"密码长度不对")];
            }else{
                if ([self.passWord.text isEqualToString:self.passWord1.text]) {
                     [self setPassWordRequest:self.passWord.text];
                }else{
                    [MBProgressHUD showError:MyString(@"您两次密码输入不一致,请重新输入")];
                }
            }
        }
        
        NSLog(@"确认支付密码");
    }else{
        if (self.passWord1.text.length==0||self.passWord.text.length==0||self.oldPassWord.text.length==0) {
            [MBProgressHUD showError:MyString(@"密码不能为空")];
        }else{
            static int i=5;
            if (i==0) {
                [MBProgressHUD showError:MyString(@"原始密码错误达上限,请次日修改")];
            }else{
                if (self.passWord.text.length<6||self.passWord1.text.length<6||self.oldPassWord.text.length<6){
                    [MBProgressHUD showError:MyString(@"密码长度不对")];
                }else{
                    if ([self.passWord.text isEqualToString:self.passWord1.text]) {
                        if ([self.passWord.text isEqualToString:self.oldPassWord.text]) {
                            [MBProgressHUD showError:MyString(@"新密码不能与原密码相同请重新设置")];
                        }else{
                            if (self.type==2) {
                                if ([self deptNumInputShouldNumber:self.passWord.text]) {
                                    [MBProgressHUD showError:@"密码不能纯数字"];
                                }else{
                                    [self exitLoginPsWord];
                                }
                                
                                
                            }else{
                                [self ModifyPassWordRequest:self.oldPassWord.text NSString:self.passWord.text];
                            }
                            
                        }
                                            }else{
                        [MBProgressHUD showError:MyString(@"您两次密码输入不一致,请重新输入")];
                    }
                }

            }
                    }
        

    }
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.passWord resignFirstResponder];
    [self.oldPassWord resignFirstResponder];
    [self.passWord1 resignFirstResponder];
}
#pragma mark -----修改登录密码-----
-(void)exitLoginPsWord{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[@"phone"]=[ZJUserDefaultsTools getCurrentUserPhone];
    dic[@"oldPassword"]=self.oldPassWord.text;
    dic[@"newPassword"]=self.passWord.text;
    [self doHttpRequestWithApi:API_changePassword method:POST dic:dic successBlock:^(NSDictionary *dic) {
        [self dismissDialog];
        [MBProgressHUD showSuccess:MyString(@"修改成功")];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            UIViewController *viewCtl = self.navigationController.viewControllers[2];
            [self.navigationController popToViewController:viewCtl animated:YES];
        });
    }];
}
-(void)ModifyPassWordRequest:(NSString *)passWord NSString:(NSString *)newPsword{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[@"oldPassword"]=passWord;
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"newPassword"]=newPsword;
    [self doHttpRequestWithApi:MODIFY method:GET dic:dic successBlock:^(NSDictionary *dic) {
        [self dismissDialog];
        [self showToast:MyString(@"设置成功")];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            UIViewController *viewCtl = self.navigationController.viewControllers[1];
            [self.navigationController popToViewController:viewCtl animated:YES];
        });
        
    }];
}
-(void)setPassWordRequest:(NSString *)passWord{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[@"password"]=passWord;
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"smsCode"]=self.cmsCode;
    [self doHttpRequestWithApi:SETCCOUNT method:GET dic:dic successBlock:^(NSDictionary *dic) {
        [self showToast:MyString(@"设置成功")];
        if (self.statue==1) {
            for(UIViewController *temVC in self.navigationController.viewControllers)
                
            {
                
                if ([temVC isKindOfClass:[MyWalletViewController class]])
                    
                {
                    
                    [self.navigationController popToViewController:temVC animated:YES];
                    
                }else if ([temVC isKindOfClass:[PayViewController class]])
                
                {
                    [self.navigationController popToViewController:temVC animated:YES];
                    
                }
                
            }
        }else if (self.statue==10){
            for(UIViewController *temVC in self.navigationController.viewControllers)
                
            {
                
                if ([temVC isKindOfClass:[PayViewController class]])
                    
                {
                    [self.navigationController popToViewController:temVC animated:YES];
                    
                }
                
            }
        }
        
    }];
}

@end
